package com.zlt.appparking.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;

public class AlipayConfig {

    //appid
    private final String APP_ID = "2021000121675864";
    //应用私钥
    private final String APP_PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCtFWirjnfNSHFPiXMt5eH3P+dnsm0ddkrIrSeymaRclX9noga+linkjkgMP+0EZUie+FxM7AFGdK7I/MrD7dsV/hpyY+PEOPIfYPqqVPYkkibUs9ztxOLyD0ZBa8xFomXRw38oh++jBZg44j30kclQe0EcrOAhSPAp/yD5t+KqTQcX8XJxzfOdU6t0nHHSvtjEvCqhBN7sucZV8ho1OxmPTOgdaA8xpfJjXeNeOVjlpR4JhhbLaxOPDdk0UMuhp9sJhUPSZIdt6CV9ae42DnM3ohoH0MBf8KkTwIiWAptSFJAdId+3DK7t9pb1VQju+5oXpL1qzL7Kuhmvp9l9J+UdAgMBAAECggEBAICYi3ALJc+Lp0Rk5wKO7dh5zkApPRDKtr1qiTgVFx0CpeolY8VuZCm/3J/dTwgZCF+0Pz+XiUsDnmkOCMiRjFlGtfePZ8Z+WMYfk53CCi8Zt2gJSngN9F/MIXKx2M9T3TqAeZVvigghO/vnfAid2GjHapUXjF6uhmFtgN6IVQwZeFwUjlMVLdKTPYXlPaGOF/YhulWiqYI3LNTVY99pb+N3Zl7Aq2wqA6daT1xaUTKrDA6dRqkUqfZIm7PxEenVaKdvN5qXgrOOvR9dTMwc18qGulqTVDs95J4z3ryQF6/qTm022LwZfz5jRfDQs+tOzDlcqrVOCpFRcBM/ByZKD7ECgYEA/ogHLN4YCdggrZ7wgWi7HScrwpX1iOfi1xk96iCizeisLV10AW9ofCeiosE0V84hjS6UlQ6B3bzlKD0qaHOLInscGoD9RfpBrDOsStmidtpYVjID3JWcjKdB9scTuQpebBuzBw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I9Bhs9Ff8P97+4eEbl625NJTE3mbYaGInhrct1X2/cHcwuIcBaodZwyQjobaeti5qVIuhECzkSlImsUQz8JEeynkepuynBkrjqV3CUoAUppIoygB0wEporXGwIDAQAB";
    //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL ="https://openapi.alipaydev.com/gateway.do";
    private final String FORMAT = "JSON";
    //签名方式
    private final String SIGN_TYPE = "RSA2";
    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
    private final String NOTIFY_URL = "http://pwtpxz.natappfree.cc/alipay/notifyUrl";
    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
    private final String RETURN_URL = "http://localhost:80/returnUrl";


    /**
     *支付宝官方提供的接口
     * @param outTradeNo 订单号
     * @param totalAmount 金额
     * @param subject  商品标题
     * @return 返回给前端
     * @throws AlipayApiException
     */
    public AlipayTradeAppPayResponse sendRequestToAlipay(String outTradeNo, Float totalAmount, String subject) throws AlipayApiException {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL,APP_ID,APP_PRIVATE_KEY,FORMAT,CHARSET,ALIPAY_PUBLIC_KEY,SIGN_TYPE);

        //设置请求参数
        AlipayTradeAppPayRequest alipayRequest = new AlipayTradeAppPayRequest ();
        alipayRequest.setReturnUrl(RETURN_URL);
        alipayRequest.setNotifyUrl(NOTIFY_URL);

//        //商品描述（可空）
        String body="123";
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\","
                + "\"total_amount\":\"" + totalAmount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
//        String result = alipayClient.sdkExecute(alipayRequest).getBody();
        AlipayTradeAppPayResponse response = alipayClient.sdkExecute(alipayRequest);
        System.out.println("是否成功：   " +  alipayClient.sdkExecute(alipayRequest).isSuccess());
        System.out.println("支付宝支付返回：    " + response.getBody());
        return response;
    }

}
